package edu.hnui.shop.commons;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.List;

/**
 * 从BindingResult中解析出异常信息
 */
public class BindingResultParseMessageUtils {

    public static String parse(BindingResult bindingResult){
        if(!bindingResult.hasErrors()){
            return "";
        }
        StringBuilder sb = new StringBuilder();

        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        fieldErrors.forEach(fe -> {
           sb.append(fe.getDefaultMessage()+",");
        });

        return sb.substring(0,sb.length()-1);
    }
}
